package com.sskj.b70.v.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.scwang.smartrefresh.header.BezierCircleHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.b70.R;
import com.sskj.b70.common.App;
import com.sskj.b70.common.adapter.BaseAdapter;
import com.sskj.b70.common.adapter.ViewHolder;
import com.sskj.b70.m.bean.FundListBean;
import com.sskj.b70.p.fragment.FundFragmentPresenter;
import com.sskj.b70.utils.DateUtil;
import com.sskj.b70.v.activity.FundManagementActivity;
import com.sskj.b70.v.base.BaseFragment;

import java.util.List;

import butterknife.BindView;

/**
 * ProjectName：new_bfex
 * DESC: (基金管理-->基金分红，挖矿产出)
 * Created by 李岩 on 2018/7/12 0012
 * updateName:(修改人名称)
 * updateTime:(修改时间)
 * updateDesc:(修改内容)
 */
public class FundManagementFtagment extends BaseFragment<FundManagementActivity, FundFragmentPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    private BaseAdapter<FundListBean.ListBean> mAdapter;
    private boolean isLoader;
    private boolean mIndexA;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_recyclerview;
    }

    @Override
    public FundFragmentPresenter getPresenter() {
        return new FundFragmentPresenter();
    }

    public static Fragment newInstence(boolean b) {
        FundManagementFtagment ftagment = new FundManagementFtagment();
        Bundle bundle = new Bundle();
        bundle.putBoolean("index", b);
        ftagment.setArguments(bundle);
        return ftagment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mIndexA = getArguments().getBoolean("index", false);
        mRefreshLayout.setRefreshHeader(new BezierCircleHeader(mActivity));

        mRefreshLayout.setOnRefreshListener(loadmorelistener -> {
            mRefreshLayout.finishRefresh();
            if (mIndexA){
                mPresenter.requestChildAtAData(true);
            }else {
                mPresenter.requestChildAtBData(true);
            }
        });
        mRefreshLayout.setOnLoadMoreListener(loadmorelistener -> {
            mRefreshLayout.finishLoadMore();
            if (mIndexA){
                mPresenter.requestChildAtAData(false);
            }else {
                mPresenter.requestChildAtBData(false);
            }
        });
        if (mIndexA) {
            mRefreshLayout.autoRefresh();
        }
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(mActivity, LinearLayoutManager.VERTICAL));

        mAdapter = new BaseAdapter<FundListBean.ListBean>(R.layout.item_fund_manage, null, mRecyclerView) {
            @Override
            public void bind(ViewHolder holder, FundListBean.ListBean item) {
                holder.setText(R.id.item_fund_output, mIndexA ? App.INSTANCE.getString(R.string.strFundManagementFtagment2) : App.INSTANCE.getString(R.string.strFundManagementFtagment3));
                holder.setText(R.id.item_fund_output_value, String.format(mIndexA ? "%s（USDT）" : "%s（BF）", mIndexA? item.getPrice() : item.getBf_num()));
                holder.setText(R.id.item_fund_time_value, DateUtil.getDateFormat(item.getAddtime() * 1000, DateUtil.PATTERN_J));
            }
        };
        mAdapter.setEmtyView();

    }

    public void onDataSuccess(List<FundListBean.ListBean> list, boolean isRefresh) {
        isLoader = true;
        if (isRefresh){
            mAdapter.setNewData(list);
        }else {
            mAdapter.addData(list);
        }
    }

    public void upDate() {
        if (!isLoader){
            mRefreshLayout.autoRefresh();
        }
    }
}
